/**
 * 
 */
package com.dream.house.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.servyou.utils.CoreUtils;
import cn.com.servyou.utils.Page;

import com.dream.house.entity.BuildDTO;
import com.dream.house.entity.BuyDTO;
import com.dream.house.entity.GroupDTO;
import com.dream.house.entity.RentDTO;
import com.dream.house.entity.RoomDTO;
import com.dream.house.entity.UserDTO;
import com.dream.house.service.BuildService;
import com.dream.house.service.BuyService;
import com.dream.house.service.RentService;
import com.dream.house.service.RoomService;
import com.dream.house.service.UserService;
import com.dream.house.utils.Constants;

/**
 * @author maoq
 *
 */
@Controller
@RequestMapping("build")
public class BuildController {
	@Autowired
	BuildService buildService;
	@Autowired
	UserService userService;
	@Autowired
	RoomService roomService;
	@Autowired
	BuyService buyService;
	@Autowired
	RentService rentService;
	
	@RequestMapping("index")
	public String index() {
		return "build/index";
	}
	
	@RequestMapping("gotoloudengji")
	public String gotoloudengji(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		return "build/lou_dengji";
	}
	
	@ResponseBody
	@RequestMapping("lou_dengji")
	public Map<String, String> dengji(BuildDTO dto, HttpServletRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			buildService.saveBuild(dto);
		} catch (Exception e) {
			map.put("error", "0");
			return map;
		}
		map.put("error", "1");
		return map;
	}
	
	@RequestMapping("fang_dengji")
	public String fangdengji(RoomDTO dto, Model model, HttpSession session) {
		try {
			roomService.saveRoom(dto);
		} catch (Exception e) {
			model.addAttribute("error", "0");
		}
		model.addAttribute("error", "1");
		return gotofangdengji(model, session);
	}
	
	@RequestMapping("gotolouchaxun")
	public String gotolouchaxun(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		return "build/lou_chaxun";
	}
	
	@RequestMapping("louquery")
	public String louquery(@ModelAttribute("dto") BuildDTO dto, Model model, int pageNo, int pageSize, String flag) {
		if ("shenpi".equals(flag)) {
			dto.setCheck_status(Constants.CHECK_STATUS_INIT);
		}
		Map<String, Object> condition = CoreUtils.entity2Map(dto);
		Page page = buildService.findByPage(condition, pageNo, pageSize);
		model.addAttribute("page", page);
		if ("shenpi".equals(flag)) {
			return "shenpi/lou_list";
		}
		return "build/lou_list";
	}
	
	@RequestMapping("gotofangdengji")
	public String gotofangdengji(Model model, HttpSession session){
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		model.addAttribute("buildList", getBuildListByGroupId(groupList.get(0).getGroup_id()));
		return "build/fang_dengji";
	}
	
	@RequestMapping("gotofangchaxun")
	public String gotofangchaxun(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		model.addAttribute("buildList", getBuildListByGroupId(groupList.get(0).getGroup_id()));
		return "build/fang_chaxun";
	}
	
	@RequestMapping("fangquery")
	public String fangquery(Model model, @ModelAttribute("dto") RoomDTO dto, int pageNo, int pageSize, String flag) {
		if ("shenpi".equals(flag)) {
			dto.setCheck_status(Constants.CHECK_STATUS_INIT);
		}
		Map<String, Object> condition = CoreUtils.entity2Map(dto);
		Page page = roomService.findByPage(condition, pageNo, pageSize);
		model.addAttribute("page", page);
		if ("shenpi".equals(flag)) {
			return "shenpi/fang_list";
		}
		return "build/fang_list";
	}
	
	private List<BuildDTO> getBuildListByGroupId(String id) {
		BuildDTO dto = new BuildDTO();
		dto.setGroup_id(id);
		dto.setCheck_status(Constants.CHECK_STATUS_PASSED);
		List<BuildDTO> builds =  buildService.findBuilds(dto);
		return builds;
	}
	
	@ResponseBody
	@RequestMapping("getBuildList")
	public List<BuildDTO> getBuildList(String id){
		List<BuildDTO> list = getBuildListByGroupId(id);
		return list;
	}
	
	@RequestMapping("gotolouedit/{id}")
	public String gotolouedit(@PathVariable String id, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		BuildDTO build = buildService.getBuild(id);
		model.addAttribute("build", build);
		return "build/lou_edit";
	}
	
	@ResponseBody
	@RequestMapping("dolouedit")
	public String dolouedit(BuildDTO build, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		try {
			buildService.modifyBuild(build);
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}
	
	@ResponseBody
	@RequestMapping("deletelou/{id}")
	public String dolouedit(@PathVariable String id, Model model) {
		try {
			buildService.removeBuild(id);
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}
	
	@RequestMapping("gotofangedit/{id}")
	public String gotofangedit(@PathVariable String id, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		RoomDTO room = roomService.getRoom(id);
		model.addAttribute("buildList", getBuildListByGroupId(room.getGroup_id()));
		model.addAttribute("room", room);
		return "build/fang_edit";
	}
	
	@RequestMapping("dofangedit")
	public String dofangedit(@ModelAttribute("room") RoomDTO room, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		model.addAttribute("buildList", getBuildListByGroupId(room.getGroup_id()));
		try {
			roomService.modifyRoom(room);
			model.addAttribute("error", "1");
		} catch (Exception e) {
			model.addAttribute("error", "1");
		}
		return "build/fang_edit";
	}
	
	@ResponseBody
	@RequestMapping("deletefang/{id}")
	public String deletefang(@PathVariable String id, Model model) {
		try {
			roomService.removeRoom(id);
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}
	
	@RequestMapping("queryAllFang")
	public String queryAllFang(String id, Model model) {
		
		RoomDTO dto = roomService.getRoom(id);
		model.addAttribute("room", dto);
		
		BuyDTO buy = new BuyDTO();
		buy.setRoom_id(id);
		List<BuyDTO> buys = buyService.getBuysByRoom(id);
		model.addAttribute("buys", buys);
		
		RentDTO rent = new RentDTO();
		rent.setRoom_id(id);
		List<RentDTO> rents = rentService.findRents(rent);
		model.addAttribute("rents", rents);
		
		return "build/allFang";
	}
	
}
